//
//  popPresentationControl.swift
//  weibo
//
//  Created by 陈经纬 on 16/7/20.
//  Copyright © 2016年 陈经纬. All rights reserved.
//

import UIKit

class popPresentationControl: UIPresentationController {
    class func popVC() ->UIViewController {
      return  NSBundle.mainBundle().loadNibNamed("popPresentationControl", owner: nil, options: nil).last as! UIViewController
    }
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    /// 保存菜单的尺寸
    var presentFrame = CGRectZero
    
    // 用于布局转场动画弹出的控件
    override func containerViewWillLayoutSubviews()
    {
        
        // 设置弹出视图的尺寸
        presentedView()?.frame = presentFrame
        
        //添加蒙板,用来监听点击让pop消失的按钮
        containerView?.insertSubview(bgBtn, atIndex: 0)
        bgBtn.addTarget(self, action: #selector(popPresentationControl.bgBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    //懒加载背景按钮
   private lazy var bgBtn: UIButton = {
        let bgBtn = UIButton()
        bgBtn.backgroundColor = UIColor.clearColor()
        bgBtn.frame = UIScreen.mainScreen().bounds
        return bgBtn
    }()
    
    
   @objc private func bgBtnClick(){
    
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
}
